import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'device_page.dart';
import 'home_page.dart';
import 'message_page.dart';
import 'mine_page.dart';

class AppHomePage extends StatefulWidget {
  const AppHomePage({super.key});

  @override
  State<StatefulWidget> createState() => _StateAppHomePage();
}

class _StateAppHomePage extends State<AppHomePage> {

  int currentIndex = 0;

  @override
  void initState() {
    super.initState();
    currentIndex = 0;
    // init();
  }


  @override
  Widget build(BuildContext context) {
    var lastPopTime = DateTime.now();
    return WillPopScope(child: Scaffold(
        body: IndexedStack(
          index: currentIndex,
          children:
          getBottomNavigatorList().map((res) => res.widget!).toList(),
        ),
        bottomNavigationBar: BottomNavigationBar(
          items: List.generate(
            getBottomNavigatorList().length,
                (index) => BottomNavigationBarItem(
              icon: index == currentIndex
                  ? getBottomNavigatorList()[index].selectedIcon
                  : getBottomNavigatorList()[index].unselectedIcon,
              label: getBottomNavigatorList()[index].title,
            ),
          ),
          currentIndex: currentIndex,
          type: BottomNavigationBarType.fixed,
          onTap: (index) {
            _changePage(index, context);
          },
          selectedFontSize: 10.w,
          selectedItemColor: Colors.blue,
          unselectedFontSize: 10.w,
          unselectedItemColor: Colors.grey,
        )
    ), onWillPop: () async {
      // 点击返回键的操作
      if (DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
        lastPopTime = DateTime.now();
        EasyLoading.showToast("再按一次退出");
        return false;
      } else {
        lastPopTime = DateTime.now();
        // 退出app
        await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
        return true;
      }
    });
  }

  //如果点击的导航页不是当前项，切换
  void _changePage(int index, BuildContext context) {
    if (index != currentIndex) {
      setState(() {
        currentIndex = index;
      });
    }
  }

  List<NavigationBarData> getBottomNavigatorList(){
    final List<NavigationBarData> bottomNavigatorList = [
      NavigationBarData(
          title: "首页",
          widget: const HomePage(),
          unselectedIcon: Stack(
            clipBehavior: Clip.none,
            children: [
              Image.asset(
                "assets/images/conversation_normal.webp",
                width: 18.w,
                height: 18.w,
              ),
              /*Positioned(
                top: -5,
                right: -6,
                child: UnconstrainedBox(
                  child: TIMUIKitConversationTotalUnread(width: 16, height: 16),
                ),
              )*/
            ],
          ),
          selectedIcon: Stack(
            clipBehavior: Clip.none,
            children: [
              Image.asset(
                "assets/images/conversation_selected.webp",
                width: 18.w,
                height: 18.w,
              ),
              /*Positioned(
                top: -5,
                right: -6,
                child: UnconstrainedBox(
                  child: TIMUIKitConversationTotalUnread(width: 16, height: 16),
                ),
              )*/
            ],
          )
      ),
      NavigationBarData(
          title: "消息",
          widget: const MessagePage(),
          unselectedIcon: Stack(
            clipBehavior: Clip.none,
            children: [
              Image.asset(
                "assets/images/contact_unselect.webp",
                width: 18.w,
                height: 18.w,
              ),
              /*Positioned(
                top: -5,
                right: -6,
                child: UnconstrainedBox(
                  child: TIMUIKitConversationTotalUnread(width: 16, height: 16),
                ),
              )*/
            ],
          ),
          selectedIcon: Stack(
            clipBehavior: Clip.none,
            children: [
              Image.asset(
                "assets/images/contact_selected.webp",
                width: 18.w,
                height: 18.w,
              ),
              /*Positioned(
                top: -5,
                right: -6,
                child: UnconstrainedBox(
                  child: TIMUIKitConversationTotalUnread(width: 16, height: 16),
                ),
              )*/
            ],
          )),
      NavigationBarData(
          title: "设备",
          widget: const DevicePage(),
          unselectedIcon: Stack(
            clipBehavior: Clip.none,
            children: [
              Image.asset(
                "assets/images/icon_nav_toolkit_unselected.png",
                width: 18.w,
                height: 18.w,
              ),
              /*Positioned(
                top: -5,
                right: -6,
                child: UnconstrainedBox(
                  child: TIMUIKitConversationTotalUnread(width: 16, height: 16),
                ),
              )*/
            ],
          ),
          selectedIcon: Stack(
            clipBehavior: Clip.none,
            children: [
              Image.asset(
                "assets/images/icon_nav_toolkit_selected.png",
                width: 18.w,
                height: 18.w,
              ),
              /*Positioned(
                top: -5,
                right: -6,
                child: UnconstrainedBox(
                  child: TIMUIKitConversationTotalUnread(width: 16, height: 16),
                ),
              )*/
            ],
          )),
      NavigationBarData(
          title: "我",
          widget: const MinePage(),
          unselectedIcon: Stack(
            clipBehavior: Clip.none,
            children: [
              Image.asset(
                "assets/images/icon_nav_find_unselected.png",
                width: 18.w,
                height: 18.w,
              ),
              /*Positioned(
                top: -5,
                right: -6,
                child: UnconstrainedBox(
                  child: TIMUIKitConversationTotalUnread(width: 16, height: 16),
                ),
              )*/
            ],
          ),
          selectedIcon: Stack(
            clipBehavior: Clip.none,
            children: [
              Image.asset(
                "assets/images/icon_nav_find_selected.png",
                width: 18.w,
                height: 18.w,
              ),
              /*Positioned(
                top: -5,
                right: -6,
                child: UnconstrainedBox(
                  child: TIMUIKitConversationTotalUnread(width: 16, height: 16),
                ),
              )*/
            ],
          ))
    ];
    return bottomNavigatorList;
  }
}


/// 底部导航栏数据对象
class NavigationBarData {
  /// 未选择时候的图标
  final Widget unselectedIcon;

  /// 选择后的图标
  final Widget selectedIcon;

  /// 标题内容
  final String title;

  /// 页面组件
  final Widget? widget;

  final ValueChanged<int>? onTap;

  final int? index;

  NavigationBarData(
      {required this.unselectedIcon,
        required this.selectedIcon,
        required this.title,
        this.widget,
        this.onTap,
        this.index});
}